package cn.img;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.qrcode.QRCodeWriter;

import javax.imageio.ImageIO;
import java.io.FileOutputStream;
import java.util.HashMap;

public class Qr1 {
    public static void main(String[] args) throws Exception {
        //String qr = "c:/qr.png";
        //生成QRcode
        //Map<EncodeHintType, Object> hints = new HashMap<>();
        //hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        //var bm = new QRCodeWriter().encode("https://img2.baidu.com/it/u=3642893371,3461783373&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500", BarcodeFormat.QR_CODE, 200, 200,hints);

        var hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        //hints.put(EncodeHintType.MARGIN,5);


        String src = """
                package cn.img;
                                
                                
                import javax.swing.*;
                import java.awt.*;
                import java.awt.geom.AffineTransform;
                import java.awt.geom.GeneralPath;
                                
                /**
                 * <p>Project: javase - Img3
                 * <p>Powered by webrx On 2021-12-17 13:02:50
                 * <p>Created by IntelliJ IDEA
                 *
                 * @author webrx [webrx@126.com]
                 * @version 1.0
                 * @since 17
                 */
                public class Ex4 extends JPanel {
                                
                    public static void main(String[] args) {
                        JFrame jFrame = new JFrame("五星红旗");
                        jFrame.getContentPane().add(new Ex4(600));
                        jFrame.pack();
                        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        jFrame.setLocationRelativeTo(null);
                        jFrame.setVisible(true);
                    }
                                
                    /**
                     * 创建一个五角星形状.
                     * 该五角星的中心坐标为(sx,sy),中心到顶点的距离为radius,其中某个顶点与中心的连线的偏移角度为theta(弧度)
                     *
                     * @return pentacle 一个☆
                     */
                    public static Shape createPentacle(double sx, double sy, double radius, double theta) {
                        final double arc = Math.PI / 5;
                        final double rad = Math.sin(Math.PI / 10) / Math.sin(3 * Math.PI / 10);
                        GeneralPath path = new GeneralPath();
                        path.moveTo(1, 0);
                        for (int i = 0; i < 5; i++) {
                            path.lineTo(rad * Math.cos((1 + 2 * i) * arc), rad * Math.sin((1 + 2 * i) * arc));
                            path.lineTo(Math.cos(2 * (i + 1) * arc), Math.sin(2 * (i + 1) * arc));
                        }
                        path.closePath();
                        AffineTransform atf = AffineTransform.getScaleInstance(radius, radius);
                        atf.translate(sx / radius, sy / radius);
                        atf.rotate(theta);
                        return atf.createTransformedShape(path);
                    }
                                
                                
                    private int width, height;
                    private double maxR = 0.15, minR = 0.05;
                    private double maxX = 0.25, maxY = 0.25;
                    private double[] minX = {0.50, 0.60, 0.60, 0.50};
                    private double[] minY = {0.10, 0.20, 0.35, 0.45};
                                
                    /**
                     * 创建一个宽度为width的国旗
                     */
                    public Ex4(int width) {
                        this.width = width / 3 * 3;
                        this.height = width / 3 * 2;
                        setPreferredSize(new Dimension(this.width, this.height));
                    }
                                
                    @Override
                    protected void paintComponent(Graphics g) {
                        Graphics2D graphics2D = (Graphics2D) g;
                                
                        //画旗面
                        graphics2D.setPaint(Color.RED);
                        graphics2D.fillRect(0, 0, width, height);
                                
                        //画大☆
                        double ox = height * maxX, oy = height * maxY;
                        graphics2D.setPaint(Color.YELLOW);
                        graphics2D.fill(createPentacle(ox, oy, height * maxR, -Math.PI / 2));
                                
                        //画小★
                        for (int i = 0; i < 4; i++) {
                            double sx = minX[i] * height, sy = minY[i] * height;
                            double theta = Math.atan2(oy - sy, ox - sx);
                            graphics2D.fill(createPentacle(sx, sy, height * minR, theta));
                        }
                    }
                }
                                
                """;

        //生成
        var bm = new QRCodeWriter().encode(src, BarcodeFormat.QR_CODE, 600, 600, hints);
        var i = MatrixToImageWriter.toBufferedImage(bm);
        ImageIO.write(i, "png", new FileOutputStream("qr.png"));
        Runtime.getRuntime().exec("cmd /k start qr.png");
    }
}
